package 富婆梦.第一阶段.Java基础视频学习.Scoket知识点;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.Scoket练习
 * @ClassName: InetAddressDemo
 * @Author: hemengjin
 * @Description: java.net.InetAddress使用
 * 表示互联网中的IP地址
 * java.net.InetAddress
 * 1、静态方法
 * static InetAddress  getLocalHost()   LocalHost本地主机
 * 返回本地主机,返回值InetAddress对象
 * static InetAddress getByName(String hostName)传递主机名,获取IP地址对象
 * 2、非静态方法
 * String getHoustAddress()获取主机IP地址
 * String getHoustName()获取主机名
 * @Date:2021/11/10 14:06
 * @Version: 1.0
 */
public class InetAddressDemo {
    public static void function() throws UnknownHostException {
        //InetAddress.getByName("www.baidu.com")通过主机名称拿到主机+IP
        //InetAddress inet = InetAddress.getByName("www.baidu.com");
        InetAddress inet = InetAddress.getByName("LAPTOP-QHPL4AH1");
        System.out.println(inet);

        InetAddress inet1 = InetAddress.getLocalHost();
        System.out.println(inet1.toString());
        System.out.println(InetAddress.getLocalHost());
        //InetAddress.getLocalHost();这个方法会返回主机名+IP地址  可通过字符串切割操作分别拿到主机名和IP地址
        String[] str = inet1.toString().split("/");
        for (String s : str) {
            System.out.println(s);
        }

        //通过InetAddress对象可以直接拿到IP或是主机名称
        String IP = inet.getHostAddress();
        String name = inet.getHostName();
        System.out.println("IP:"+IP+"  NAME:"+name);
    }

    public static void main(String[] args) throws UnknownHostException {
        function();
    }
}
